<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Intrusive treap based associative containers: treap_set, treap_multiset and treap</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../intrusive.html" title="Chapter&#160;14.&#160;Boost.Intrusive">
<link rel="prev" href="sg_set_multiset.html" title="Intrusive scapegoat tree based associative containers: sg_set, sg_multiset and sgtree">
<link rel="next" href="advanced_lookups_insertions.html" title="Advanced lookup and insertion functions for associative containers">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="sg_set_multiset.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../intrusive.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="advanced_lookups_insertions.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="intrusive.treap_set_multiset"></a><a class="link" href="treap_set_multiset.html" title="Intrusive treap based associative containers: treap_set, treap_multiset and treap">Intrusive treap based associative
    containers: treap_set, treap_multiset and treap</a>
</h2></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="treap_set_multiset.html#intrusive.treap_set_multiset.treap_set_multiset_hooks">Using
      binary search tree hooks: bs_set_base_hook and bs_set_member_hook</a></span></dt>
<dt><span class="section"><a href="treap_set_multiset.html#intrusive.treap_set_multiset.treap_set_multiset_containers">treap_set,
      treap_multiset and treap containers</a></span></dt>
<dt><span class="section"><a href="treap_set_multiset.html#intrusive.treap_set_multiset.treap_set_exceptions">Exception
      safety of treap-based intrusive containers</a></span></dt>
<dt><span class="section"><a href="treap_set_multiset.html#intrusive.treap_set_multiset.treap_set_multiset_example">Example</a></span></dt>
</dl></div>
<p>
      The name <span class="emphasis"><em>treap</em></span> is a mixture of <span class="emphasis"><em>tree</em></span>
      and <span class="emphasis"><em>heap</em></span> indicating that Treaps exhibit the properties
      of both binary search trees and heaps. A treap is a binary search tree that
      orders the nodes by a key but also by a priority attribute. The nodes are ordered
      so that the keys form a binary search tree and the priorities obey the max
      heap order property.
    </p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem">
          If v is a left descendant of u, then key[v] &lt; key[u];
        </li>
<li class="listitem">
          If v is a right descendant of u, then key[v] &gt; key[u];
        </li>
<li class="listitem">
          If v is a child of u, then priority[v] &lt;= priority[u];
        </li>
</ul></div>
<p>
      If priorities are non-random, the tree will usually be unbalanced; this worse
      theoretical average-case behavior may be outweighed by better expected-case
      behavior, as the most important items will be near the root. This means most
      important objects will be retrieved faster than less important items and for
      items keys with equal keys most important objects will be found first. These
      properties are important for some applications.
    </p>
<p>
      The priority comparison will be provided just like the key comparison, via
      a function object that will be stored in the intrusive container. This means
      that the priority can be stored in the value to be introduced in the treap
      or computed on flight (via hashing or similar).
    </p>
<p>
      <span class="bold"><strong>Boost.Intrusive</strong></span> offers 3 containers based
      on treaps: <code class="computeroutput"><a class="link" href="../boost/intrusive/treap_set.html" title="Class template treap_set">treap_set</a></code>,
      <code class="computeroutput"><a class="link" href="../boost/intrusive/treap_multiset.html" title="Class template treap_multiset">treap_multiset</a></code>
      and <code class="computeroutput"><a class="link" href="../boost/intrusive/treap.html" title="Class template treap">treap</a></code>. The first two
      are similar to <code class="computeroutput"><a class="link" href="../boost/intrusive/set.html" title="Class template set">set</a></code> or <code class="computeroutput"><a class="link" href="../boost/intrusive/multiset.html" title="Class template multiset">multiset</a></code> and the latter is a generalization
      that offers functions both to insert unique and multiple keys.
    </p>
<p>
      The memory overhead of these containers with Boost.Intrusive hooks is 3 pointers.
    </p>
<p>
      An empty, <code class="computeroutput"><a class="link" href="../boost/intrusive/treap_set.html" title="Class template treap_set">treap_set</a></code>,
      <code class="computeroutput"><a class="link" href="../boost/intrusive/treap_multiset.html" title="Class template treap_multiset">treap_multiset</a></code>
      or <code class="computeroutput"><a class="link" href="../boost/intrusive/treap.html" title="Class template treap">treap</a></code> has also the
      size of 3 pointers and an integer (supposing empty function objects for key
      and priority comparison and constant-time size).
    </p>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="intrusive.treap_set_multiset.treap_set_multiset_hooks"></a><a class="link" href="treap_set_multiset.html#intrusive.treap_set_multiset.treap_set_multiset_hooks" title="Using binary search tree hooks: bs_set_base_hook and bs_set_member_hook">Using
      binary search tree hooks: bs_set_base_hook and bs_set_member_hook</a>
</h3></div></div></div>
<p>
        <code class="computeroutput"><a class="link" href="../boost/intrusive/treap_set.html" title="Class template treap_set">treap_set</a></code>, <code class="computeroutput"><a class="link" href="../boost/intrusive/treap_multiset.html" title="Class template treap_multiset">treap_multiset</a></code> and <code class="computeroutput"><a class="link" href="../boost/intrusive/treap.html" title="Class template treap">treap</a></code> don't use their own hooks
        but plain binary search tree hooks. This has many advantages since binary
        search tree hooks can also be used to insert values in splay containers and
        scapegoat trees.
      </p>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="special">...</span><span class="identifier">Options</span><span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">bs_set_base_hook</span><span class="special">;</span>
</pre>
<div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
            <code class="computeroutput"><a class="link" href="../boost/intrusive/bs_set_base_hook.html" title="Class template bs_set_base_hook">bs_set_base_hook</a></code>:
            the user class derives publicly from this class to make it compatible
            with scapegoat tree based containers.
          </li></ul></div>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="special">...</span><span class="identifier">Options</span><span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">bs_set_member_hook</span><span class="special">;</span>
</pre>
<div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
            <code class="computeroutput"><a class="link" href="../boost/intrusive/set_member_hook.html" title="Class template set_member_hook">set_member_hook</a></code>:
            the user class contains a public member of this class to make it compatible
            with scapegoat tree based containers.
          </li></ul></div>
<p>
        <code class="computeroutput"><a class="link" href="../boost/intrusive/bs_set_base_hook.html" title="Class template bs_set_base_hook">bs_set_base_hook</a></code>
        and <code class="computeroutput"><a class="link" href="../boost/intrusive/bs_set_member_hook.html" title="Class template bs_set_member_hook">bs_set_member_hook</a></code>
        receive the same options explained in the section <a class="link" href="usage.html" title="How to use Boost.Intrusive">How
        to use Boost.Intrusive</a>:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem">
            <span class="bold"><strong><code class="computeroutput"><span class="identifier">tag</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Tag</span><span class="special">&gt;</span></code></strong></span>
            (for base hooks only): This argument serves as a tag, so you can derive
            from more than one base hook. Default: <code class="computeroutput"><span class="identifier">tag</span><span class="special">&lt;</span><span class="identifier">default_tag</span><span class="special">&gt;</span></code>.
          </li>
<li class="listitem">
            <span class="bold"><strong><code class="computeroutput"><span class="identifier">link_mode</span><span class="special">&lt;</span><span class="identifier">link_mode_type</span>
            <span class="identifier">LinkMode</span><span class="special">&gt;</span></code></strong></span>:
            The linking policy. Default: <code class="computeroutput"><span class="identifier">link_mode</span><span class="special">&lt;</span><span class="identifier">safe_link</span><span class="special">&gt;</span></code>.
          </li>
<li class="listitem">
            <span class="bold"><strong><code class="computeroutput"><span class="identifier">void_pointer</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">VoidPointer</span><span class="special">&gt;</span></code></strong></span>:
            The pointer type to be used internally in the hook and propagated to
            the container. Default: <code class="computeroutput"><span class="identifier">void_pointer</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">*&gt;</span></code>.
          </li>
</ul></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="intrusive.treap_set_multiset.treap_set_multiset_containers"></a><a class="link" href="treap_set_multiset.html#intrusive.treap_set_multiset.treap_set_multiset_containers" title="treap_set, treap_multiset and treap containers">treap_set,
      treap_multiset and treap containers</a>
</h3></div></div></div>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <span class="special">...</span><span class="identifier">Options</span><span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">treap_set</span><span class="special">;</span>

<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <span class="special">...</span><span class="identifier">Options</span><span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">treap_multiset</span><span class="special">;</span>

<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <span class="special">...</span><span class="identifier">Options</span><span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">treap</span><span class="special">;</span>
</pre>
<p>
        These containers receive the same options explained in the section <a class="link" href="usage.html" title="How to use Boost.Intrusive">How to use Boost.Intrusive</a>:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem">
            <span class="bold"><strong><code class="computeroutput"><span class="identifier">base_hook</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Hook</span><span class="special">&gt;</span></code></strong></span>
            / <span class="bold"><strong><code class="computeroutput"><span class="identifier">member_hook</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Hook</span><span class="special">,</span> <span class="identifier">Hook</span> <span class="identifier">T</span><span class="special">::*</span> <span class="identifier">PtrToMember</span><span class="special">&gt;</span></code></strong></span>
            / <span class="bold"><strong><code class="computeroutput"><span class="identifier">value_traits</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">ValueTraits</span><span class="special">&gt;</span></code></strong></span>:
            To specify the hook type or value traits used to configure the container.
            (To learn about value traits go to the section <a class="link" href="value_traits.html" title="Containers with custom ValueTraits">Containers
            with custom ValueTraits</a>.)
          </li>
<li class="listitem">
            <span class="bold"><strong><code class="computeroutput"><span class="identifier">constant_time_size</span><span class="special">&lt;</span><span class="keyword">bool</span> <span class="identifier">Enabled</span><span class="special">&gt;</span></code></strong></span>:
            To activate the constant-time <code class="computeroutput"><span class="identifier">size</span><span class="special">()</span></code> operation. Default: <code class="computeroutput"><span class="identifier">constant_time_size</span><span class="special">&lt;</span><span class="keyword">true</span><span class="special">&gt;</span></code>
          </li>
<li class="listitem">
            <span class="bold"><strong><code class="computeroutput"><span class="identifier">size_type</span><span class="special">&lt;</span><span class="keyword">bool</span> <span class="identifier">Enabled</span><span class="special">&gt;</span></code></strong></span>:
            To specify the type that will be used to store the size of the container.
            Default: <code class="computeroutput"><span class="identifier">size_type</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span><span class="special">&gt;</span></code>
          </li>
</ul></div>
<p>
        And they also can receive additional options:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem">
            <span class="bold"><strong><code class="computeroutput"><span class="identifier">compare</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Compare</span><span class="special">&gt;</span></code></strong></span>:
            Comparison function for the objects to be inserted in containers. The
            comparison functor must induce a strict weak ordering. Default: <code class="computeroutput"><span class="identifier">compare</span><span class="special">&lt;</span>
            <span class="identifier">std</span><span class="special">::</span><span class="identifier">less</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;</span></code>
          </li>
<li class="listitem">
            <span class="bold"><strong><code class="computeroutput"><span class="identifier">priority</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">PriorityCompare</span><span class="special">&gt;</span></code></strong></span>:
            Priority Comparison function for the objects to be inserted in containers.
            The comparison functor must induce a strict weak ordering. Default:
            <code class="computeroutput"><span class="identifier">priority</span><span class="special">&lt;</span>
            <span class="identifier">priority_compare</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;</span></code>
          </li>
</ul></div>
<p>
        The default <code class="computeroutput"><span class="identifier">priority_compare</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>
        object function will call an unqualified function <code class="computeroutput"><span class="identifier">priority_order</span></code>
        passing two constant <code class="computeroutput"><span class="identifier">T</span></code> references
        as arguments and should return true if the first argument has higher priority
        (it will be searched faster), inducing strict weak ordering. The function
        will be found using ADL lookup so that the user just needs to define a <code class="computeroutput"><span class="identifier">priority_order</span></code> function in the same namespace
        as his class:
      </p>
<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">MyType</span>
<span class="special">{</span>
   <span class="keyword">friend</span> <span class="keyword">bool</span> <span class="identifier">priority_order</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">MyType</span> <span class="special">&amp;</span><span class="identifier">a</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">MyType</span> <span class="special">&amp;</span><span class="identifier">b</span><span class="special">)</span>
   <span class="special">{...}</span>
<span class="special">};</span>
</pre>
<p>
        or
      </p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">mytype</span> <span class="special">{</span>

<span class="keyword">struct</span> <span class="identifier">MyType</span><span class="special">{</span> <span class="special">...</span> <span class="special">};</span>

<span class="keyword">bool</span> <span class="identifier">priority_order</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">MyType</span> <span class="special">&amp;</span><span class="identifier">a</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">MyType</span> <span class="special">&amp;</span><span class="identifier">b</span><span class="special">)</span>
<span class="special">{...}</span>

<span class="special">}</span>  <span class="comment">//namespace mytype {</span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="intrusive.treap_set_multiset.treap_set_exceptions"></a><a class="link" href="treap_set_multiset.html#intrusive.treap_set_multiset.treap_set_exceptions" title="Exception safety of treap-based intrusive containers">Exception
      safety of treap-based intrusive containers</a>
</h3></div></div></div>
<p>
        In general, intrusive containers offer strong safety guarantees, but treap
        containers must deal with two possibly throwing functors (one for value ordering,
        another for priority ordering). Moreover, treap erasure operations require
        rotations based on the priority order function and this issue degrades usual
        <code class="computeroutput"><span class="identifier">erase</span><span class="special">(</span><span class="identifier">const_iterator</span><span class="special">)</span></code>
        no-throw guarantee. However, intrusive offers the strongest possible behaviour
        in these situations. In summary:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem">
            If the priority order functor does not throw, treap-based containers,
            offer exactly the same guarantees as other tree-based containers.
          </li>
<li class="listitem">
            If the priority order functor throws, treap-based containers offer strong
            guarantee.
          </li>
</ul></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="intrusive.treap_set_multiset.treap_set_multiset_example"></a><a class="link" href="treap_set_multiset.html#intrusive.treap_set_multiset.treap_set_multiset_example" title="Example">Example</a>
</h3></div></div></div>
<p>
        Now let's see a small example using both hooks and <code class="computeroutput"><a class="link" href="../boost/intrusive/treap_set.html" title="Class template treap_set">treap_set</a></code>/
        <code class="computeroutput"><a class="link" href="../boost/intrusive/treap_multiset.html" title="Class template treap_multiset">treap_multiset</a></code>
        containers:
      </p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">intrusive</span><span class="special">/</span><span class="identifier">treap_set</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">vector</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">algorithm</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>

<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">intrusive</span><span class="special">;</span>

<span class="keyword">class</span> <span class="identifier">MyClass</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">bs_set_base_hook</span><span class="special">&lt;&gt;</span> <span class="comment">//This is a base hook</span>
<span class="special">{</span>
   <span class="keyword">int</span> <span class="identifier">int_</span><span class="special">;</span>
   <span class="keyword">unsigned</span> <span class="keyword">int</span> <span class="identifier">prio_</span><span class="special">;</span>

   <span class="keyword">public</span><span class="special">:</span>
   <span class="comment">//This is a member hook</span>
   <span class="identifier">bs_set_member_hook</span><span class="special">&lt;&gt;</span> <span class="identifier">member_hook_</span><span class="special">;</span>

   <span class="identifier">MyClass</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span><span class="special">,</span> <span class="keyword">unsigned</span> <span class="keyword">int</span> <span class="identifier">prio</span><span class="special">)</span>  <span class="special">:</span>  <span class="identifier">int_</span><span class="special">(</span><span class="identifier">i</span><span class="special">),</span> <span class="identifier">prio_</span><span class="special">(</span><span class="identifier">prio</span><span class="special">)</span>
      <span class="special">{}</span>

   <span class="keyword">unsigned</span> <span class="keyword">int</span> <span class="identifier">get_priority</span><span class="special">()</span> <span class="keyword">const</span>
   <span class="special">{</span>  <span class="keyword">return</span> <span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">prio_</span><span class="special">;</span>   <span class="special">}</span>

   <span class="comment">//Less and greater operators</span>
   <span class="keyword">friend</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&lt;</span> <span class="special">(</span><span class="keyword">const</span> <span class="identifier">MyClass</span> <span class="special">&amp;</span><span class="identifier">a</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">MyClass</span> <span class="special">&amp;</span><span class="identifier">b</span><span class="special">)</span>
      <span class="special">{</span>  <span class="keyword">return</span> <span class="identifier">a</span><span class="special">.</span><span class="identifier">int_</span> <span class="special">&lt;</span> <span class="identifier">b</span><span class="special">.</span><span class="identifier">int_</span><span class="special">;</span>  <span class="special">}</span>
   <span class="keyword">friend</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&gt;</span> <span class="special">(</span><span class="keyword">const</span> <span class="identifier">MyClass</span> <span class="special">&amp;</span><span class="identifier">a</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">MyClass</span> <span class="special">&amp;</span><span class="identifier">b</span><span class="special">)</span>
      <span class="special">{</span>  <span class="keyword">return</span> <span class="identifier">a</span><span class="special">.</span><span class="identifier">int_</span> <span class="special">&gt;</span> <span class="identifier">b</span><span class="special">.</span><span class="identifier">int_</span><span class="special">;</span>  <span class="special">}</span>
   <span class="comment">//Default priority compare</span>
   <span class="keyword">friend</span> <span class="keyword">bool</span> <span class="identifier">priority_order</span> <span class="special">(</span><span class="keyword">const</span> <span class="identifier">MyClass</span> <span class="special">&amp;</span><span class="identifier">a</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">MyClass</span> <span class="special">&amp;</span><span class="identifier">b</span><span class="special">)</span>
      <span class="special">{</span>  <span class="keyword">return</span> <span class="identifier">a</span><span class="special">.</span><span class="identifier">prio_</span> <span class="special">&lt;</span> <span class="identifier">b</span><span class="special">.</span><span class="identifier">prio_</span><span class="special">;</span>  <span class="special">}</span>  <span class="comment">//Lower value means higher priority</span>
   <span class="comment">//Inverse priority compare</span>
   <span class="keyword">friend</span> <span class="keyword">bool</span> <span class="identifier">priority_inverse_order</span> <span class="special">(</span><span class="keyword">const</span> <span class="identifier">MyClass</span> <span class="special">&amp;</span><span class="identifier">a</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">MyClass</span> <span class="special">&amp;</span><span class="identifier">b</span><span class="special">)</span>
      <span class="special">{</span>  <span class="keyword">return</span> <span class="identifier">a</span><span class="special">.</span><span class="identifier">prio_</span> <span class="special">&gt;</span> <span class="identifier">b</span><span class="special">.</span><span class="identifier">prio_</span><span class="special">;</span>  <span class="special">}</span>  <span class="comment">//Higher value means higher priority</span>
<span class="special">};</span>

<span class="keyword">struct</span> <span class="identifier">inverse_priority</span>
<span class="special">{</span>
   <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">()(</span><span class="keyword">const</span> <span class="identifier">MyClass</span> <span class="special">&amp;</span><span class="identifier">a</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">MyClass</span> <span class="special">&amp;</span><span class="identifier">b</span><span class="special">)</span> <span class="keyword">const</span>
   <span class="special">{</span>  <span class="keyword">return</span> <span class="identifier">priority_inverse_order</span><span class="special">(</span><span class="identifier">a</span><span class="special">,</span> <span class="identifier">b</span><span class="special">);</span> <span class="special">}</span>
<span class="special">};</span>


<span class="comment">//Define an treap_set using the base hook that will store values in reverse order</span>
<span class="keyword">typedef</span> <span class="identifier">treap_set</span><span class="special">&lt;</span> <span class="identifier">MyClass</span><span class="special">,</span> <span class="identifier">compare</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">greater</span><span class="special">&lt;</span><span class="identifier">MyClass</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="special">&gt;</span>     <span class="identifier">BaseSet</span><span class="special">;</span>

<span class="comment">//Define an multiset using the member hook that will store</span>
<span class="keyword">typedef</span> <span class="identifier">member_hook</span><span class="special">&lt;</span><span class="identifier">MyClass</span><span class="special">,</span> <span class="identifier">bs_set_member_hook</span><span class="special">&lt;&gt;,</span> <span class="special">&amp;</span><span class="identifier">MyClass</span><span class="special">::</span><span class="identifier">member_hook_</span><span class="special">&gt;</span> <span class="identifier">MemberOption</span><span class="special">;</span>
<span class="keyword">typedef</span> <span class="identifier">treap_multiset</span>
   <span class="special">&lt;</span> <span class="identifier">MyClass</span><span class="special">,</span> <span class="identifier">MemberOption</span><span class="special">,</span> <span class="identifier">priority</span><span class="special">&lt;</span><span class="identifier">inverse_priority</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">MemberMultiset</span><span class="special">;</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
   <span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">MyClass</span><span class="special">&gt;::</span><span class="identifier">iterator</span> <span class="identifier">VectIt</span><span class="special">;</span>

   <span class="comment">//Create several MyClass objects, each one with a different value</span>
   <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">MyClass</span><span class="special">&gt;</span> <span class="identifier">values</span><span class="special">;</span>
   <span class="keyword">for</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="number">100</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span>  <span class="identifier">values</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">MyClass</span><span class="special">(</span><span class="identifier">i</span><span class="special">,</span> <span class="special">(</span><span class="identifier">i</span> <span class="special">%</span> <span class="number">10</span><span class="special">)));</span>

   <span class="identifier">BaseSet</span> <span class="identifier">baseset</span><span class="special">;</span>
   <span class="identifier">MemberMultiset</span> <span class="identifier">membermultiset</span><span class="special">;</span>

   <span class="comment">//Now insert them in the sets</span>
   <span class="keyword">for</span><span class="special">(</span><span class="identifier">VectIt</span> <span class="identifier">it</span><span class="special">(</span><span class="identifier">values</span><span class="special">.</span><span class="identifier">begin</span><span class="special">()),</span> <span class="identifier">itend</span><span class="special">(</span><span class="identifier">values</span><span class="special">.</span><span class="identifier">end</span><span class="special">());</span> <span class="identifier">it</span> <span class="special">!=</span> <span class="identifier">itend</span><span class="special">;</span> <span class="special">++</span><span class="identifier">it</span><span class="special">){</span>
      <span class="identifier">baseset</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(*</span><span class="identifier">it</span><span class="special">);</span>
      <span class="identifier">membermultiset</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(*</span><span class="identifier">it</span><span class="special">);</span>
   <span class="special">}</span>

   <span class="comment">//Now test treap_sets</span>
   <span class="special">{</span>
      <span class="identifier">BaseSet</span><span class="special">::</span><span class="identifier">reverse_iterator</span> <span class="identifier">rbit</span><span class="special">(</span><span class="identifier">baseset</span><span class="special">.</span><span class="identifier">rbegin</span><span class="special">()),</span> <span class="identifier">rbitend</span><span class="special">(</span><span class="identifier">baseset</span><span class="special">.</span><span class="identifier">rend</span><span class="special">());</span>
      <span class="identifier">MemberMultiset</span><span class="special">::</span><span class="identifier">iterator</span> <span class="identifier">mit</span><span class="special">(</span><span class="identifier">membermultiset</span><span class="special">.</span><span class="identifier">begin</span><span class="special">()),</span> <span class="identifier">mitend</span><span class="special">(</span><span class="identifier">membermultiset</span><span class="special">.</span><span class="identifier">end</span><span class="special">());</span>
      <span class="identifier">VectIt</span> <span class="identifier">it</span><span class="special">(</span><span class="identifier">values</span><span class="special">.</span><span class="identifier">begin</span><span class="special">()),</span> <span class="identifier">itend</span><span class="special">(</span><span class="identifier">values</span><span class="special">.</span><span class="identifier">end</span><span class="special">());</span>

      <span class="comment">//Test the objects inserted in the base hook treap_set</span>
      <span class="keyword">for</span><span class="special">(;</span> <span class="identifier">it</span> <span class="special">!=</span> <span class="identifier">itend</span><span class="special">;</span> <span class="special">++</span><span class="identifier">it</span><span class="special">,</span> <span class="special">++</span><span class="identifier">rbit</span><span class="special">)</span>
         <span class="keyword">if</span><span class="special">(&amp;*</span><span class="identifier">rbit</span> <span class="special">!=</span> <span class="special">&amp;*</span><span class="identifier">it</span><span class="special">)</span>   <span class="keyword">return</span> <span class="number">1</span><span class="special">;</span>

      <span class="comment">//Test the objects inserted in the member hook treap_set</span>
      <span class="keyword">for</span><span class="special">(</span><span class="identifier">it</span> <span class="special">=</span> <span class="identifier">values</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span> <span class="identifier">it</span> <span class="special">!=</span> <span class="identifier">itend</span><span class="special">;</span> <span class="special">++</span><span class="identifier">it</span><span class="special">,</span> <span class="special">++</span><span class="identifier">mit</span><span class="special">)</span>
         <span class="keyword">if</span><span class="special">(&amp;*</span><span class="identifier">mit</span> <span class="special">!=</span> <span class="special">&amp;*</span><span class="identifier">it</span><span class="special">)</span> <span class="keyword">return</span> <span class="number">1</span><span class="special">;</span>

      <span class="comment">//Test priority order</span>
      <span class="keyword">for</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="number">100</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">){</span>
         <span class="keyword">if</span><span class="special">(</span><span class="identifier">baseset</span><span class="special">.</span><span class="identifier">top</span><span class="special">()-&gt;</span><span class="identifier">get_priority</span><span class="special">()</span> <span class="special">!=</span> <span class="keyword">static_cast</span><span class="special">&lt;</span><span class="keyword">unsigned</span> <span class="keyword">int</span><span class="special">&gt;(</span><span class="identifier">i</span><span class="special">/</span><span class="number">10</span><span class="special">))</span>
            <span class="keyword">return</span> <span class="number">1</span><span class="special">;</span>
         <span class="keyword">if</span><span class="special">(</span><span class="identifier">membermultiset</span><span class="special">.</span><span class="identifier">top</span><span class="special">()-&gt;</span><span class="identifier">get_priority</span><span class="special">()</span> <span class="special">!=</span> <span class="number">9u</span> <span class="special">-</span> <span class="keyword">static_cast</span><span class="special">&lt;</span><span class="keyword">unsigned</span> <span class="keyword">int</span><span class="special">&gt;(</span><span class="identifier">i</span><span class="special">/</span><span class="number">10</span><span class="special">))</span>
            <span class="keyword">return</span> <span class="number">1</span><span class="special">;</span>
         <span class="identifier">baseset</span><span class="special">.</span><span class="identifier">erase</span><span class="special">(</span><span class="identifier">baseset</span><span class="special">.</span><span class="identifier">top</span><span class="special">());</span>
         <span class="identifier">membermultiset</span><span class="special">.</span><span class="identifier">erase</span><span class="special">(</span><span class="identifier">membermultiset</span><span class="special">.</span><span class="identifier">top</span><span class="special">());</span>
      <span class="special">}</span>
   <span class="special">}</span>
   <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2005 Olaf Krzikalla<br>Copyright &#169; 2006-2012 Ion Gaztanaga<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="sg_set_multiset.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../intrusive.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="advanced_lookups_insertions.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
